<?php
namespace app\user\controller;
use app\admin\controller\Ajax;
use think\Db;
use think\facade\Request;
use Qrcode\Qrcode;
use app\admin\model\Users as UsersModel;
class Set extends Common{
    protected $uid;
    public function initialize()
    {
        parent::initialize();
        $this->uid=session('user.id');
    }	
	
	
    public function index(){
        if(request()->isPost()){
            $data = input('post.');
            $user = db('users');
            $oldEmail = $user->where('id',$this->uid)->value('email');
            if(Db::name('users')->where([['email','=',$data['email']],['id','neq',$this->uid]])->find()){
                return ['status'=>0,'msg'=>'该邮箱已被注册！'];
            }

            if($oldEmail != $data['email']){
                $data['email_validated'] = 0;
            }
            if (Db::name('users')->where('id',$this->uid)->update($data)!==false) {
                $result['msg'] = '编辑资料成功!';
                $result['status'] = 1;
            } else {
                $result['msg'] = '编辑资料失败!';
                $result['status'] = 0;
            }
            return $result;
        }else{
            $province = Db::name('Region')->where ('pid',1)->select ();
            $this->assign('province',$province);
            $city = Db::name('Region')->where ( 'pid',$this->userInfo['province'])->select ();
            $this->assign('city',$city);
            $district = Db::name('Region')->where ('pid',$this->userInfo['city'])->select ();
            $this->assign('district',$district);
            $this->assign('title','基本设置');
            return $this->fetch();
        }
    }
 /*   public function getRegion(){
        $list=Db::name("region")->where('pid',input("pid"))->select();
        return $list;
    }*/
    public function getRegion(){
        $Region=db("region");
        $pid = input("pid");
        $arr = explode(':',$pid);
        $map['pid']=$arr[1];
        $list=$Region->where($map)->select();
        return $list;
    }
    public function avatar(){
        $data = input('post.');
        db('users')->where(['id'=>$this->uid])->update($data);
        return true;
    }
    /**
     * 修改密码
     * @param $old_password  旧密码
     * @param $new_password  新密码
     * @param $confirm_password 确认新 密码
     */
    public function repass(){
        $old_password  = input('post.nowpass');
        $new_password = input('post.pass');
        $confirm_password = input('post.repass');

        if(strlen($new_password) < 6)
            return array('status'=>0,'msg'=>'密码不能低于6位字符');
        if($new_password != $confirm_password)
            return array('status'=>0,'msg'=>'两次密码输入不一致');
        //验证原密码
        if(($this->userInfo['password'] != '' && md5($old_password) != $this->userInfo['password']))
            return array('status'=>0,'msg'=>'密码验证失败');
        if(db('users')->where("id", $this->uid)->update(array('password'=>md5($new_password)))!==false){

            return array('status'=>1,'msg'=>'修改成功','action'=>url('index'));
        }else{
            return array('status'=>0,'msg'=>'修改失败');
        }
    }
    public function unbind(){
        db('oauth')->where("uid",$this->uid)->delete();
        session('user.qq','0');
        return array('status'=>1,'msg'=>'QQ已解绑','action'=>url('index'));
    }
	public function tixian(){
		//获取所有的用户信息
		$user=db('users')->where('id='.$_SESSION['think']['user']['id'])->find();
        $type=input('get.type');
		if(Request::isAjax()){
		    $data=input('post.');
            if(empty($data['bank_name'])){
                return array('code'=>-1,'msg'=>'请先完善银行信息，再申请');
            }
		    if($data['num']<0||empty($data['num'])){
                return array('code'=>0,'msg'=>'请输入正确的金额');
            }
            $type=$data['titype'];
            if($type==1){
                $money=$user['money_jifen'];
            }elseif($type==2){
                $money=$user['money_qian'];
            }elseif($type==3){
                $money=$user['money_benjin'];
            }elseif($type==4){
                $money=$user['money_qita'];
            }elseif($type==5){
                $money=$user['money_baodan'];
            }else{
                $money=0;
            }
            if($data['num']>$money){
                return array('code'=>0,'msg'=>'当前余额不足，请勿操作！');
            }
            $sys=getsystem();
            $time=unserialize($sys['tixian_xianzhi']);
            $xingqi = date("w");
            $shijian = date("H");
            if(empty($time)){
                return array('code'=>0,'msg'=>'当前没有可提现的日期');
            }
            if(in_array($xingqi, $time) || in_array(8, $time)) {
                if($shijian<$sys['txtimemin']||$sys['txtimemax']<$shijian){
                    return array('code'=>0,'msg'=>'抱歉！当前时间不可提现, 请在'.$sys['txtimemin'].'点至'.$sys['txtimemax'].'点申请提现');exit;
                }
            }else {
                return array('code'=>0,'msg'=>'抱歉！当前日期不可提现');exit;
            }
            if($data['num']<$sys['min_tixian']){
                return array('code'=>0,'msg'=>'抱歉！最小提现额为：'.$sys['min_tixian']);exit;
            }
            if($data['num']%$sys['beishu']!=0){
                return array('code'=>0,'msg'=>'抱歉！提现倍数为：'.$sys['beishu']);exit;
            }
            $type=$data['titype'];
            $num=$data['num'];
            //扣除手续费
         //   $tixianfei=db('system')->where('id=1')->value('tixian_fei');

            $fen1=$sys['tixian_fei']/100*$num;
            $level=getulevel(1);
            if($user['money_max']>$level['b12']){
                $fei2=$sys['shouxufei']/100*$num;
            }else{
                $fei2=0;
            }
            $fen=$fen1+$fei2;
            if($type==1){
                $shifa_qian=$num-$fen ;
            }

            //  jifen($type,$userid,$num，$source,$jiajian)
            //  type          1为积分    2为 金钱    3 为本金  4 为其他
            //  $source       1商城消费  2 会员提现  3会员转账  4 管理变动  5 手动充值  6 微信充值
            //  num           数值
            //  $jiajian     1 为 加       2为减
            //  返回结果     1、执行成功   2 余额不足

            $jieguo=jifen($type,$user['id'],$num,2,2,'');
            if($jieguo==1){
                //提现成功，写进提现表
                $tt['userid']=$user['id'];
                $tt['type']=$type;
                $tt['tixian_time']=time();
                $tt['shoutype']=$data['shoutype'];
                $tt['num']=$num;
                $tt['shifa_qian']=$shifa_qian;
                db('tixian')->insert($tt);
                return array('code'=>1,'msg'=>'恭喜提现申请成功');
            }
        }
		$this->assign('user',$user);
		//获取所有允许提现的列表
		$tixianlist=db('money_config')->where('tixian=1')->select();
		$this->assign('tixian',$tixianlist);
		$this->assign('type',$type);
		$this->assign('type1',1);
		$this->assign('type2',2);
		$this->assign('type3',3);
		$this->assign('type4',4);

		
		
		return view();
	}
	public function tixian_record(){
		
		$userid=$_SESSION['think']['user']['id'];
		$lists = db('tixian')->where('userid='.$userid)->order('tixian_time desc')->paginate(20);
		
			 
		$page = $lists->render();
		$this->assign('page', $page);
        $this->assign('lists', $lists);
         $this->fetch();
		 return view();
		
		
	}
	/*
	 *会员充值
	 */
	public  function chongzhi(){
        $uid=$this->uid;
        $user=getuser($uid);
	    if(Request::isAjax()){
            $data=input('post.');
            if(empty($data['mey'])||$data['mey']<0){
                return array('code'=>0,'msg'=>'请输入正确的充值金额');
            }
            $count=db('users')->where('nickname',$data['nickname'])->count();
            if($count<=0){
                return array('code'=>0,'msg'=>'该会员不存在');
            }
            $array=array(
                'uid'=>$user['id'],
                'nickname'=>$user['nickname'],
                'username'=>$user['username'],
                'jine'=>$data['mey'],
                'lx'=>$data['chongtype'],
                'cdate'=>time(),
                'pzimg'=>$data['pzimg'],
                'isgrant'=>1
            );
            db('chongzhi')->insert($array);
            return array('code'=>1,'msg'=>'充值申请成功!');
        }
        $chongxian=db('money_config')->where('chongzhi',1)->select();
        $this->assign('chongxian', $chongxian);
        $sys=getsystem();
        $this->assign('sys', $sys);
        $this->assign('user', $user);
        return view();
    }
    /*
     *充值记录
     */
    public function chongzhi_log(){
        $sys=getsystem();
        $userid=$_SESSION['think']['user']['id'];
        $lists = db('chongzhi')->where(array('uid'=>$userid))->order('cdate desc')->paginate(20);
        $page = $lists->render();
        $this->assign('sys', $sys);
        $this->assign('page', $page);
        $this->assign('lists', $lists);
        $this->fetch();

        return view();

    }
    public function dingdan(){
        $userid=$_SESSION['think']['user']['id'];
        $is_ok = input('is_ok');
        if(isset($is_ok)){
            if($is_ok==2){
                $lists = db('shop_dingdan')->where('userid='.$userid.' and (is_ok=5 or is_ok=2 )')->order('time desc')->paginate(20);
                $select=$is_ok;
            }else{
                $lists = db('shop_dingdan')->where(array('userid'=>$userid,'is_ok'=>$is_ok))->order('time desc')->paginate(20);
                $select=$is_ok;
            }
        }
        if(!empty($lists)){
            $page = $lists->render();
            $this->assign('page', $page);
            $this->fetch();
        }
        $this->assign('select', $select);
        $this->assign('lists', $lists);
        return view();

    }

	//删除订单
	public  function del_order(){
	    $id=input('id');
        $userid=$_SESSION['think']['user']['id'];
        $order=db('shop_dingdan')->where(array('id'=>$id))->find();
        if($order['userid']==$userid){
            if($order['pay_ok']==0){
                Db('shop_dingdan')->where('id',$id)->delete();
                $this->redirect('/user/');
            }
        }
    }
    //原点升级
    public function uplevel(){
        //获取当前信息
        $userid=$_SESSION['think']['user']['id'];
        $user=db('users')->where('id',$userid)->find();
        $this->assign('user', $user);
        $user_level=db('user_level')->where('level_id','gt',0)->select();
        $this->assign('user_level', $user_level);

        if(Request::isAjax()) {

            $data = input('post.');
            $up_level=getulevel($data['uplevel']);
            $level=getulevel($user['level']);
            if($level['level']>=$up_level['level']){
                return array('code'=>0,'msg'=>'请选择正确的级别');
            }
            $cha=$up_level['lsk']-$level['lsk'];
            if($data['type_level']==2){
                if($user['money_jifen']<$cha){
                    return array('code'=>0,'msg'=>'当前余额不足，申请失败');
                }else{
                    $beizhu="升级";
                    jifen(1,$userid,$cha,19,2,$beizhu);//先把自己的钱扣掉
                }
            }
            $array=array(
                'userid'=>$userid,
                'level'=>$level['level'],
                'uplevel'=>$up_level['level'],
                'cha'=>$cha,
                'createtime'=>time(),
                'status'=>0,
                'type_level'=>$data['type_level'],
            );
            db('uplevel')->insert($array);
            return array('code'=>1,'msg'=>'申请成功');
        }
        return view();
    }
    //提现验证二级密码
    public  function  check(){
        $id=$_SESSION['think']['user']['id'];
        $user=getuser($id);
        $type=input('type');
        if(Request::isAjax()){
            $data=input('post.');
            if(md5($data['pwd'])==$user['twopassword']){
                $_SESSION['users'][$user['nickname']]['password']=$user['twopassword'];
                if($type==1){
                    return  array('code'=>1,'msg'=>'验证成功','url'=>url('zhuan'));
                }elseif($type==2){
                    return  array('code'=>1,'msg'=>'验证成功','url'=>url('daili'));
                }elseif($type==3){
                    return  array('code'=>1,'msg'=>'验证成功','url'=>url('guamai'));
                }elseif($type==4){
                    return  array('code'=>1,'msg'=>'验证成功','url'=>url('mybank'));
                }

            }else{
                return  array('code'=>0,'msg'=>'验证失败');
            }
        }
        $this->assign('type',$type);
        return $this->fetch();
    }
    function tuiguang(){
        $userid=$_SESSION['think']['user']['id'];
        $sys=getsystem();
       // $url="http://www.yulianmian.com/user/login/reg";
        $str= "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=".$sys['url']."?id=".$userid;
        $this->assign('str',$str);
        $this->assign('bg',$sys['bg']);
        return view();
    }
    //升级记录
    public function uplevel_record(){
        $userid=$_SESSION['think']['user']['id'];

        $lists = db('uplevel')
            ->where('userid',$userid)
            ->order('createtime desc')
            ->paginate(20);
        $page = $lists->render();

        $this->assign('page', $page);
        $this->assign('lists', $lists);

        $this->fetch();

        return view();

    }
    //推荐结构
    public function tuijian(){

        $userid=$_SESSION['think']['user']['id'];
        $user=getuser($userid);
        $this->assign('user', $user);
        return view();
    }
	public function daili(){
        $levellist=db('user_level')->where('level_id','gt',0)->select();
        $this->assign('levels',$levellist);
        $user=db('users')->where('id',$this->uid)->find();
        $this->assign('user',$user);
        $sys=getsystem();
        $this->assign('sys',$sys);
        $bianhao=rand(100000,999999);
        $this->assign('bianhao',$bianhao);
        if($_SESSION['users'][$user['nickname']]['password']==null||$_SESSION['users'][$user['nickname']]['password']!=$user['twopassword']){
            $this->redirect('check',array('type'=>2));
        }
        $inf=db('users')->where('id',$this->uid)->find();
        $this->assign('inf',json_encode($inf,true));
        $province = db('Region')->where ( array('pid'=>1) )->select ();
        $this->assign('province',json_encode($province,true));
        $city = db('Region')->where ( array('pid'=>$inf['province']) )->select ();
        $this->assign('city',json_encode($city,true));
        $district = db('Region')->where ( array('pid'=>$inf['city']) )->select ();
        $this->assign('district',json_encode($district,true));
        if(Request::isAjax()) {
            $data = input('post.');
            $username = $data['username'];
            $mobile = $data['mobile'];
            $nickname = $data['nickname'];
            $password = $data['password'];
            $twopassword = $data['twopassword'];
            $bank_name = $data['bank_name'];
            $bank_dizhi = $data['bank_dizhi'];
            $bank_card = $data['bank_card'];
            $topmobi = $data['topmobi'];
            $centermobile = $data['center_id'];
            $shenfenzheng = $data['shenfenzheng'];
            $fathername = $data['fathername'];
            $treeplace = $data['treeplace'];
            $level = $data['level'];
            $info['level']=1;
            if(!$nickname || !$password){
                return array('code'=>0,'msg'=>'请填写账号或密码');
            }
       /*     if(empty($username )){
                return array('code'=>-1,'msg'=>'请输入姓名');
            }
            $info['username']=$username;*/
            if($sys['code']=='open'){
                if(empty($data['code'])){
                    return array('code'=>-1,'msg'=>'请输入验证码');
                }
                $check=$_SESSION['check'][$mobile];
                if($check!=$data['code']){
                    return array('code' => 0, 'msg' => '验证码不正确');
                }
            }
         /*   $province =explode(':',$data['province']);
            $data['province'] = isset( $province[1])?$province[1]:'';
            $city =explode(':',$data['city']);
            $data['city'] = isset( $city[1])?$city[1]:'';
            $district =explode(':',$data['district']);
            $data['district'] = isset( $district[1])?$district[1]:'';

            if($data['province']==null ||$data['city']==null ||$data['district']==null){
                return array('code'=>0,'msg'=>'请选择联系地址');
            }
            $info['province']=$data['province'];
            $info['city']=$data['city'];
            $info['district']=$data['district'];
            if(empty( $data['dizhi'] )){
                return array('code'=>-1,'msg'=>'请输入收货地址');
            }
            $info['dizhi']= $data['dizhi'];*/


/*
            if(empty($shenfenzheng )){
                return array('code'=>-1,'msg'=>'请输入身份证号');
            }
            $info['shenfenzheng']=$shenfenzheng;*/
            $user = db('users')->where("nickname",$nickname)->find();
            if($user){
                return array('code'=>0,'msg'=>'账号已存在!');
            }
            $info['nickname']=$nickname;
            $user = db('users')->where("mobile",$mobile)->find();
            if($user){
                return array('code'=>0,'msg'=>'手机号已存在!');
            }
                $info['mobile']=$mobile;

            if(empty($password)){
                return array('code'=>0,'msg'=>'密码不能为空!');
            }else{
                $alen=strlen($password);
                if($alen<6||$alen>12){
                    return array('code'=>-1,'msg'=>'请输入6-12位的密码');
                }
                $info['password']=md5($password);
            }
            if(empty($twopassword)){
                return array('code'=>0,'msg'=>'二级密码不能为空!');
            }else{
                $alen=strlen($twopassword);
                if($alen<6||$alen>12){
                    return array('code'=>-1,'msg'=>'请输入6-12位的密码');
                }
                $info['twopassword']=md5($twopassword);
            }

            if(empty($topmobi)){
                return array('code'=>0,'msg'=>'推荐人编号不能为空！！！');
            }else{
                $reus = db('users')->where(array('nickname'=>$topmobi,'activation'=>1))->find();
                if(empty($reus)){
                    return array('code'=>0,'msg'=>'推荐人不存在或者未激活！');
                }else{
                    $rpath="".$reus['rpath'].$reus['id'].",";
                    $info['up_userid']=$reus['id'];
                    $info['relevel']=$reus['relevel']+1;
                    $info['rpath']=$rpath;
                }
            }
            if($sys['anzhi']=='open') {
                if (empty($fathername)) {
                    return array('code' => 0, 'msg' => '安置人编号不能为空！！！');
                } else {
                    $father = db('users')->where(array('nickname' => $fathername, 'activation' => 1))->find();
                    if (empty($father)) {
                        return array('code' => 0, 'msg' => '安置人不存在或者未激活！');
                    } else {
                        $is_user = db('users')->where('fatherid', $father['id'])->where('treeplace', $treeplace)->find();
                        if (empty($is_user)) {
                            $ppath = "" . $father['ppath'] . $father['id'] . ",";
                            $info['fatherid'] = $father['id'];
                            $info['ppath'] = $ppath;
                            $info['plevel'] = $father['plevel'] + 1;
                        } else {
                            return array('code' => 0, 'msg' => '该位置已有会员，请更换位置后注册！');
                        }

                    }
                }
            }
          /*  if(empty($centermobile)){
                return array('code'=>0,'msg'=>'服务中心编号不能为空！！！');
            }else{
                $server = db('users')->where(array('nickname'=>$centermobile,'activation'=>1,'server'=>2))->find();
                if(empty($server)){
                    return array('code'=>0,'msg'=>'服务中心不存在或者未激活！');
                }else{
                    $info['center_id']=$server['id'];
                }
            }*/
            $info['reg_time'] = time();
            $info['treeplace'] = $treeplace;
            $uid = db('users')->insertGetId($info);
            db('users')->where('id',$uid)->setField('uid',$uid);
            if($sys['anzhi']=='open') {
                if ($treeplace == 1) {
                    db('users')->where('id', $father['id'])->setField('leftid', $uid);
                } else {
                    db('users')->where('id', $father['id'])->setField('rightid', $uid);
                }
            }
            return array('code'=>1,'msg'=>'注册成功','url' => url('index/index'));

        }
        return view();
	}
	public function zhuan(){
        //获取当前信息
        $userid=$_SESSION['think']['user']['id'];
        $user=db('users')->where('id='.$userid)->find();
        $this->assign('user', $user);
        if($_SESSION['users'][$user['nickname']]['password']==null||$_SESSION['users'][$user['nickname']]['password']!=$user['twopassword']){
            $this->redirect('check',array('type'=>1));
        }
        //获取允许转账类型
        $qiantype=db('money_config')->where('zhuan=1')->select();
        $this->assign('qiantype',$qiantype);
        $this->assign('type1',1);
        $this->assign('type2',2);
        $this->assign('type3',3);
        $this->assign('type4',4);
        $this->assign('type5',5);
        if(Request::isAjax()) {
            $data = input('post.');
            if(empty($data['usermobile'])){
                return array('code'=>0,'msg'=>'请输入转账会员编号');
            }else{
                $buser=db('users')->where(array('nickname'=>$data['usermobile']))->find();
                if(empty($buser)){
                    return array('code'=>0,'msg'=>'该会员不存在');
                }
            }
            if(empty($data['num'])||$data['num']<0){
                return array('code'=>0,'msg'=>'请输入正确的金额');
            }
            $beishu=db('system')->where('id=1')->value('zhuanbeishu');
            if($data['num']%$beishu!=0){
                return array('code'=>0,'msg'=>'转账金额为'.$beishu.'倍数');
            }
            if($data['type']==1){
                if($user['money_jifen']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==2){
                if($user['money_qian']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==3){
                if($user['money_benjin']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==4){
                if($user['money_qita']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==5){
                if($user['money_baodan']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }
            $to_userid=db('users')->where('nickname',$data['usermobile'])->value('id');
                //  jifen($type,$userid,$num，$source,$jiajian,$beizhu)
                //  type          1为积分    2为 金钱    3 为本金  4 为其他
                //  $source       1商城消费  2 会员提现  3会员转账  4 管理变动  5 手动充值  6 微信充值
                //  num           数值
                //  $jiajian     1 为 加       2为减
                //  返回结果     1、执行成功   2 余额不足
                $beizhu='对方编号：'.$data['usermobile'];
                $zhuan=jifen($data['type'],$userid,$data['num'],3,2,$beizhu);//先把自己的钱扣掉
                if($zhuan==1){
                    //增加给对方
                    //根据手机号查询对方的会员ID
                    $usermobile=db('users')->where('id',$_SESSION['think']['user']['id'])->value('nickname');
                    $beizhu='对方编号：'.$usermobile;
                    jifen($data['type'],$to_userid,$data['num'],3,1,$beizhu);//把对应的钱加给别人
                    return array('code'=>1,'msg'=>'转账成功');
                }
        }
		 return view();
	}

    /*
 * 转换
 *
 */
    public function zhuanhuan(){
        //获取当前信息
        $userid=$_SESSION['think']['user']['id'];
        $user=db('users')->where('id='.$userid)->find();
        $this->assign('user', $user);
        if($_SESSION['users'][$user['nickname']]['password']==null||$_SESSION['users'][$user['nickname']]['password']!=$user['twopassword']){
            $this->redirect('check',array('type'=>1));
        }
        //获取允许转账类型
        $qiantype=db('money_config')->where('zhuan=1')->select();
        $this->assign('qiantype',$qiantype);
        $this->assign('type1',1);
        $this->assign('type2',2);
        $this->assign('type3',3);
        $this->assign('type4',4);
        $this->assign('type5',5);
        if(Request::isAjax()) {
            $data = input('post.');
            if(empty($data['num'])||$data['num']<0){
                return array('code'=>0,'msg'=>'请输入正确的金额');
            }
            $beishu=db('system')->where('id=1')->value('zhuanbeishu');
            if($data['num']%$beishu!=0){
                return array('code'=>0,'msg'=>'转换金额为'.$data['num'].'倍数');
            }
            if($data['type']==1){
                if($user['money_jifen']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==2){
                if($user['money_qian']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==3){
                if($user['money_benjin']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==4){
                if($user['money_qita']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }elseif($data['type']==5){
                if($user['money_baodan']<$data['num']){
                    return array('code'=>0,'msg'=>'余额不足，请勿操作');
                }
            }
            //  jifen($type,$userid,$num，$source,$jiajian,$beizhu)
            //  type          1为积分    2为 金钱    3 为本金  4 为其他
            //  $source       1商城消费  2 会员提现  3会员转账  4 管理变动  5 手动充值  6 微信充值
            //  num           数值
            //  $jiajian     1 为 加       2为减
            //  返回结果     1、执行成功   2 余额不足
            $beizhu='对方编号：'.$user['nickname'];
            $zhuan=jifen($data['type'],$userid,$data['num'],20,2,$beizhu);//先把自己的钱扣掉
            if($zhuan==1){
                //增加给对方
                //根据手机号查询对方的会员ID
                $beizhu='对方编号：'.$user['nickname'];
                jifen(5,$user['id'],$data['num'],20,1,$beizhu);//把对应的钱加给别人
                return array('code'=>1,'msg'=>'转换成功');
            }
        }
        return view();
    }

    public function zhuanhuan_record(){
        $userid=$_SESSION['think']['user']['id'];
        $lists = db('money_log')->where('userid='.$userid)->where('source=20')->order('time desc')->paginate(20);


        $page = $lists->render();
        $this->assign('page', $page);
        $this->assign('lists', $lists);
        $this->fetch();

        return view();

    }
    public function guamai(){
        //获取当前信息
        $userid=$_SESSION['think']['user']['id'];
        $user=db('users')->where('id='.$userid)->find();
        $this->assign('user', $user);
        if($_SESSION['users'][$user['nickname']]['password']==null||$_SESSION['users'][$user['nickname']]['password']!=$user['twopassword']){
            $this->redirect('check',array('type'=>3));
        }
        //获取允许转账类型
        $qiantype=db('money_config')->where('zhuan',1)->select();
        $this->assign('qiantype',$qiantype);
        if(Request::isAjax()) {
            $data = input('post.');
            $sys=getsystem();
            $time=strtotime(date("Y-m-d 00:00:00"));
            $count=db('guamai')->where('uid',$user['id'])->where('createtime','gt',$time)->count();

            if($sys['guamai_num']<=$count){
                return array('code'=>0,'msg'=>'当日只能交易'.$sys['guamai_num'].'次');
            }
            if(empty($data['num'])||$data['num']<0){
                return array('code'=>0,'msg'=>'请输入正确的数量');
            }
            if(empty($data['price'])||$data['price']<0){
                return array('code'=>0,'msg'=>'请输入正确的单价');
            }
            if($user['money_jifen']<$data['num']){
                return array('code'=>0,'msg'=>'余额不足，请勿操作');
            }
            if($data['num']<$sys['guamai_min']){
                return array('code'=>0,'msg'=>'挂卖最小数量为'.$sys['guamai_min']);
            }
            if($data['num']>$sys['max_guamai']){
                return array('code'=>0,'msg'=>'挂卖最大数量为'.$sys['max_guamai']);
            }
            if($data['num']%$sys['guamai_bei']!=0){
                return array('code'=>0,'msg'=>'挂卖为'.$sys['guamai_bei'].'的倍数');
            }
            $sys=getsystem();
            $sxf=$data['num']*$sys['guamai_fei']/100;
            $dt=array(
                'uid'=>$userid,
                'num'=>$data['num'],
                'price'=>$data['price'],
                'sxf'=>$sxf,
                'unickname'=>$user['nickname'],
                'createtime'=>time()
            );
            $is_ok=db('guamai')->insert($dt);
            if($is_ok){
                db('users')->where('id',$userid)->setDec('money_jifen',$data['num']);
            }
            return array('code'=>1,'msg'=>'挂卖成功');

        }
        return view();
    }
    //  我的购买
    public function guamai_buy(){
        $userid=$_SESSION['think']['user']['id'];
        $lists=db('guamai')->where(array('gid'=>$userid))->order('createtime desc')->paginate(20);;
        $page = $lists->render();

        $this->assign('page', $page);
        $this->assign('lists',$lists);
        $this->fetch();
        return view();

    }
    public function guamai_my_pay(){
        $id=input('id');
        $guamailist=db('guamai')->where('id',$id)->find();
        $data=array(
            'is_ok'=>3,
        );
        db('guamai')->where('id',$guamailist['id'])->update($data);
        $result=array('success'=>1,'msg'=>'确认成功');
        echo    json_encode($result);exit;
    }
    //挂卖记录
    public function guamai_record(){
        $sys=getsystem();
        $userid=$_SESSION['think']['user']['id'];
        $lists = db('guamai')->where(array('uid'=>$userid))->order('createtime desc')->paginate(20);
        $page = $lists->render();
        $this->assign('sys', $sys);
        $this->assign('page', $page);
        $this->assign('lists', $lists);
        $this->fetch();

        return view();

    }

    public function guamai_pay(){
        $id=input('id');
        $guamailist=db('guamai')->where('id',$id)->find();
        $sj=$guamailist['num']-$guamailist['sxf'];
        $is_ok=db('users')->where('id',$guamailist['gid'])->setInc('money_jifen',$sj);
        if($is_ok==true){
            $data=array(
                'is_ok'=>2,
            );
            db('guamai')->where('id',$guamailist['id'])->update($data);
            $result=array('success'=>1,'msg'=>'确认成功');
            echo    json_encode($result);
        }else{
            $result=array('success'=>0,'msg'=>'确认失败');
            echo    json_encode($result);
        }
    }
    //挂卖撤销
    public function chexiao(){
        $id=input('id');
        $guamailist=db('guamai')->where('id',$id)->find();
        if($guamailist['is_ok']==0){
            $is_ok=db('users')->where('id',$guamailist['uid'])->setInc('money_jifen',$guamailist['num']);
            if($is_ok==true){
                db('guamai')->where('id',$guamailist['id'])->delete();
                $result=array('success'=>1,'msg'=>'撤销成功');
                echo    json_encode($result);
            }else{
                $result=array('success'=>0,'msg'=>'撤销成功');
                echo    json_encode($result);
            }
        }else{
            $result=array('success'=>0,'msg'=>'当前状态不能撤销');
            echo    json_encode($result);
        }

    }

	public function zhuan_record(){
		$userid=$_SESSION['think']['user']['id'];
		$lists = db('money_log')->where('userid='.$userid)->where('source=3')->order('time desc')->paginate(20);
		
			 
		$page = $lists->render();
		$this->assign('page', $page);
        $this->assign('lists', $lists);
         $this->fetch();
		
		 return view();
		
	}
	public function zhuan_show(){
		//获取转账详情
		$id=input('id');
		$info=db('money_log')->where('id='.$id)->find();
	
		$this->assign('info',$info);
		
		 return view();
		
	}
	public function jihuo(){
		$userid=$_SESSION['think']['user']['id'];
		$lists=db('users')->where(array('center_id'=>$userid,'activation'=>0))->order('reg_time desc')->paginate(20);;
		$page = $lists->render();

		$this->assign('page', $page);
		$this->assign('lists',$lists);
		$usesr=db('users')->where('id='.$userid)->find();
        $level=getulevel($usesr['level']);
		$this->assign('user',$usesr);
		$this->assign('level_name',$level['level_name']);

		 $this->fetch();
		 return view();
		
	}
    function ads(){
        $userid=$_SESSION['think']['user']['id'];
        $sys=getsystem();
        $str= "http://api.k780.com:88/?app=qr.get&data=".$sys['url']."?id=".$userid;
        $this->assign('str',$str);
        $this->assign('sys',$sys);
        return view();
    }
	public function jihuouser(){
		$userid=$_SESSION['think']['user']['id'];
		$server=getuser($userid);
        $id=input('id');
		//获取会员信息
		$user=db('users')->where('id',$id)->find();
		$level=getulevel($user['level']);
		if($server['money_baodan']<$level['lsk']){
            $result=array('success'=>0,'msg'=>'您的余额不足，请充值后再试');
            echo    json_encode($result);
        }else{
            if($user['activation']==1){
                $result=array('success'=>0,'msg'=>'会员已激活，请勿重复激活');
                echo    json_encode($result);
            }else{
                jifen(5,$userid,$level['lsk'],8,2,$user['mobile']);
                baodanjiang($user['center_id'],$level['lsk'],$user['mobile']);
                db('users')->where('id',$id)->setInc('activation');
                jihuo($id,1);

                $result=array('success'=>1,'msg'=>'激活成功');
                echo    json_encode($result);
            }
        }
	}
    public function jihuo_record(){
        $userid=$_SESSION['think']['user']['id'];
        $lists = db('money_log')->where(array('userid'=>$userid,'source'=>8))->order('time desc')->paginate(20);
        $page = $lists->render();
        $this->assign('page', $page);
        $this->assign('lists', $lists);
        $this->fetch();

        return view();

    }
	public function pay(){
		
		
		 return view();
		
	}
	public function dizhi(){
		//获取当前用户信息
		$userid=$_SESSION['think']['user']['id'];
		$user=db('users')->where('id='.$userid)->find();
		$this->assign('user',$user);
		
		 return view();
		
	}
    public function mybank($id=''){
        $userid=$_SESSION['think']['user']['id'];
        //获取当前用户信息
        $user=db('users')->where('id='.$userid)->find();
  /*      if($_SESSION['users'][$user['nickname']]['password']==null||$_SESSION['users'][$user['nickname']]['password']!=$user['twopassword']){
            $this->redirect('check',array('type'=>4));
        }*/
        /*三级联动
   */
        $province = db('Region')->where ( array('pid'=>1) )->select ();
        $user_level=db('user_level')->order('sort')->select();
        $info = UsersModel::get($userid);
        $this->assign('info',json_encode($info,true));
        $this->assign('title',lang('edit').lang('user'));
        $this->assign('province',json_encode($province,true));
        $this->assign('user_level',json_encode($user_level,true));


        $city = db('Region')->where ( array('pid'=>$info['province']) )->select ();
        $this->assign('city',json_encode($city,true));
        $district = db('Region')->where ( array('pid'=>$info['city']) )->select ();
        $this->assign('district',json_encode($district,true));
        if(Request::isAjax()){
            $data=input('post.');
           /* if(empty($data['bank_dizhi'])){
                return array('code'=>0,'msg'=>'请输入开户地址!');
            }
            if(empty($data['bank_card'])){
                return array('code'=>0,'msg'=>'请输入开户银行账号!');
            }
            if(empty($data['username'])){
                return array('code'=>0,'msg'=>'请输入开户姓名!');
            }
            if(empty($data['bank_name'])){
                return array('code'=>0,'msg'=>'请输入开户银行!');
            }*/

            $province =explode(':',$data['province']);
            $data['province'] = isset( $province[1])?$province[1]:'';
            $city =explode(':',$data['city']);
            $data['city'] = isset( $city[1])?$city[1]:'';
            $district =explode(':',$data['district']);
            $data['district'] = isset( $district[1])?$district[1]:'';

            if($data['province']==null ||$data['city']==null ||$data['district']==null){
                return array('code'=>0,'msg'=>'请选择联系地址');
            }

            if($data['password']!=$user['password']){
                $alen=strlen($data['password']);
                if($alen<6||$alen>32){
                    return array('code'=>-1,'msg'=>'请输入6-32位的密码');
                }
                $data['password']=md5($data['password']);
            }
            if($data['twopassword']!=$user['twopassword']){

                $alen=strlen($data['twopassword']);
                if($alen<6||$alen>32){
                    return array('code'=>-1,'msg'=>'请输入6-12位的二级密码');
                }
                $data['twopassword']=md5($data['twopassword']);
            }
            unset($data['file']);
            db('users')->where(['id'=>$this->uid])->update($data);
            return array('code'=>1,'msg'=>'保存成功！');
        }

        $this->assign('users', json_encode($user,true));
        $this->assign('user',$user);

        return view();

    }
	public function pay_record(){
		
		
		 return view();
		
	}
	public function baodan(){
        if(Request::isAjax()) {
            $data = input('post.');
            if(empty($data['num'])||$data['num']<0){
                return array('code'=>0,'msg'=>'请输入正确的报单数量');
            }
            if(floor($data['num'])!=$data['num']){
                return array('code'=>0,'msg'=>'请输入正确的报单数量1');
            }
            $info['baodan_time']=time();
            $info['num']=input('num');
            $info['userid']=$_SESSION['think']['user']['id'];
            db('baodan')->insert($info);
            return array('code'=>1,'msg'=>'报单成功，请等待审核');
        }
		 return view();
	}
	public function baodan_record(){
			
		$userid=$_SESSION['think']['user']['id'];
		$lists = db('baodan')->where('userid='.$userid)->order('baodan_time desc')->paginate(20);
		
			 
		$page = $lists->render();
		$this->assign('page', $page);
      
        $this->assign('lists', $lists);
         $this->fetch();
		  return view();
		
	}
	//奖金明细
    public  function  mingxi(){
        $userid=$_SESSION['think']['user']['id'];
        $lists = db('money_log')->where('userid='.$userid)->order('time desc')->paginate(20);
        $page = $lists->render();
        $this->assign('page', $page);

        $this->assign('lists', $lists);
        $this->fetch();
        return view();
    }
    /*
    *复投
    */
    public function redelivery(){
        $userid=$this->uid;
        $user=getuser($userid);
        if(Request::isAjax()){
            $data = input('post.');
            $level=getulevel($user['level']);
            if($data['type_level']==2){
                if($user['money_jifen']<$data['mey']){
                    return array('code'=>0,'msg'=>'当前余额不足，申请失败');
                }else{
                    $beizhu="复投";
                    jifen(1,$userid,$data['mey'],15,2,$beizhu);//先把自己的钱扣掉
                }
            }
            $maxmey=$data['num']*$level['b4'];
            $array=array(
                'uid'=>$userid,
                'nickname'=>$user['nickname'],
                'mey'=>$data['num'],
                'createtime'=>time(),
                'maxmey'=>$maxmey,
            );
            db('redelivery')->insert($array);
            return array('code'=>1,'msg'=>'申请成功');

        }
        return view();
    }
    //复投明细
    public  function  redelivery_log(){
        $userid=$_SESSION['think']['user']['id'];
        $lists = db('redelivery')->where('uid',$userid)->order('createtime desc')->paginate(20);
        $page = $lists->render();
        $this->assign('page', $page);
        $this->assign('lists', $lists);
        $this->fetch();
        return view();
    }
    function  tuandui(){
        $userid=input('id');
        if(empty($userid)){
            $userid=$_SESSION['think']['user']['id'];
        }
        $lists = db('users')
            ->alias('u')
            ->join('__USER_LEVEL__ ul','ul.level = u.level','right')
            ->field('u.*,ul.level_name')
            ->where('up_userid',$userid)
            ->order('jihuo_time desc')
            ->paginate(10);
        $page = $lists->render();
        $this->assign('page', $page);
        $this->assign('lists', $lists);
        $this->fetch();
        return view();
    }
   /* function tuiguang(){

        return view();
    }*/
    //申请服务中心
    function  application_service(){
        $userid=$_SESSION['think']['user']['id'];
        $user=db('users')->where('id='.$userid)->find();
        if(Request::isAjax()) {
//            $data = input('post.');
//            if(empty($data['num'])||$data['num']<0){
//                return array('code'=>0,'msg'=>'请输入正确的报单数量');
//            }
//            if(floor($data['num'])!=$data['num']){
//                return array('code'=>0,'msg'=>'请输入正确的报单数量1');
//            }
            $info['server']=1;
            db('users')->where('id',$userid)->update($info);
            return array('code'=>1,'msg'=>'申请成功，请等待审核');
        }
        $this->assign('user', $user);
        return view();
    }
    /*
     *公告
     */
    function content(){
        $news=db('article')->where('catid',1)->select();
        $title="新闻公告";
        $this->assign('news', $news);
        $this->assign('title', $title);
        return view();
    }    /*
     *公告内页
     */
    function content_show(){
        $id=input('id');
        $news=db('article')->where('catid',1)->where('id',$id)->find();
        $title="公告详情";
        $this->assign('news', $news);
        $this->assign('title', $title);
        return view();
    }
    function duanxin(){
        /*
        ***聚合数据（JUHE.CN）短信API服务接口PHP请求示例源码
        ***DATE:2015-05-25
    */
        header('content-type:text/html;charset=utf-8');
        $mobile=input('mobile');

        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $code=rand('1000','9999');
        $smsConf = array(
            'key'   => '424aa6720b2ffa5c24399d94dc91a1af', //您申请的APPKEY
            'mobile'    =>$mobile, //接受短信的用户手机号码
            'tpl_id'    => '150452', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' =>'#code#='.$code //您设置的模板变量，根据实际情况修改
            //  'tpl_value' =>'#code#=1234&#company#=聚合数据' //您设置的模板变量，根据实际情况修改
        );
        session_start();
        $_SESSION['check'][$mobile]=$code;
        $content = juhecurl($sendUrl,$smsConf,1); //请求发送短信

        if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){
                //状态为0，说明短信发送成功
                //     echo "短信发送成功,短信ID：".$result['result']['sid'];
                echo  json_encode(array('success'=>1,'msg'=>'短信发送成功'));exit;
            }else{
                //状态非0，说明失败
                $msg = $result['reason'];
                echo  json_encode(array('success'=>0,'msg'=>'短信发送失败'));exit;
                //     echo "短信发送失败(".$error_code.")：".$msg;
            }
        }else{
            //返回内容异常，以下可根据业务逻辑自行修改
            //   echo "请求发送短信失败";
            echo  json_encode(array('success'=>0,'msg'=>'请求发送短信失败'));exit;
        }
    }

}